import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../controllers/profile_controller.dart';

class ProfilePage extends StatelessWidget {
  const ProfilePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ProfileController>(
      init: ProfileController(),
      builder: (controller) {
        return Scaffold(
          appBar: AppBar(
            title: const Text('个人资料'),
            actions: [
              IconButton(
                icon: const Icon(Icons.edit),
                onPressed: () => Get.toNamed('/profile/edit'),
              ),
            ],
          ),
          body: SafeArea(
            child: SingleChildScrollView(
              padding: EdgeInsets.all(16.w),
              child: Column(
                children: [
                  // 头像区域
                  Container(
                    width: double.infinity,
                    padding: EdgeInsets.all(20.w),
                    decoration: BoxDecoration(
                      color: Theme.of(context).colorScheme.surface,
                      borderRadius: BorderRadius.circular(12.r),
                      boxShadow: [
                        BoxShadow(
                          color: Colors.black.withOpacity(0.05),
                          blurRadius: 10.r,
                          offset: Offset(0, 2.h),
                        ),
                      ],
                    ),
                    child: Column(
                      children: [
                        CircleAvatar(
                          radius: 50.r,
                          backgroundColor: Theme.of(context).colorScheme.primary,
                          child: Icon(
                            Icons.person,
                            size: 50.w,
                            color: Colors.white,
                          ),
                        ),
                        SizedBox(height: 16.h),
                        Text(
                          '用户名',
                          style: TextStyle(
                            fontSize: 20.sp,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                        SizedBox(height: 4.h),
                        Text(
                          'user@example.com',
                          style: TextStyle(
                            fontSize: 14.sp,
                            color: Colors.grey[600],
                          ),
                        ),
                      ],
                    ),
                  ),
                  
                  SizedBox(height: 24.h),
                  
                  // 个人信息
                  _buildInfoSection(
                    context,
                    title: '个人信息',
                    items: [
                      _buildInfoItem(
                        context,
                        icon: Icons.phone,
                        title: '手机号',
                        value: '138****8888',
                      ),
                      _buildInfoItem(
                        context,
                        icon: Icons.cake,
                        title: '生日',
                        value: '1990-01-01',
                      ),
                      _buildInfoItem(
                        context,
                        icon: Icons.location_on,
                        title: '地址',
                        value: '北京市朝阳区',
                      ),
                    ],
                  ),
                  
                  SizedBox(height: 24.h),
                  
                  // 账户设置
                  _buildInfoSection(
                    context,
                    title: '账户设置',
                    items: [
                      _buildInfoItem(
                        context,
                        icon: Icons.security,
                        title: '账户安全',
                        value: '已开启',
                        showArrow: true,
                        onTap: () => controller.openAccountSecurity(),
                      ),
                      _buildInfoItem(
                        context,
                        icon: Icons.privacy_tip,
                        title: '隐私设置',
                        value: '',
                        showArrow: true,
                        onTap: () => controller.openPrivacySettings(),
                      ),
                      _buildInfoItem(
                        context,
                        icon: Icons.notifications,
                        title: '消息通知',
                        value: '',
                        showArrow: true,
                        onTap: () => controller.openNotificationSettings(),
                      ),
                    ],
                  ),
                  
                  SizedBox(height: 24.h),
                  
                  // 其他功能
                  _buildInfoSection(
                    context,
                    title: '其他',
                    items: [
                      _buildInfoItem(
                        context,
                        icon: Icons.help,
                        title: '帮助与反馈',
                        value: '',
                        showArrow: true,
                        onTap: () => controller.openHelp(),
                      ),
                      _buildInfoItem(
                        context,
                        icon: Icons.info,
                        title: '关于我们',
                        value: '',
                        showArrow: true,
                        onTap: () => controller.openAbout(),
                      ),
                    ],
                  ),
                  
                  SizedBox(height: 32.h),
                  
                  // 退出登录按钮
                  SizedBox(
                    width: double.infinity,
                    child: ElevatedButton(
                      onPressed: () => controller.logout(),
                      style: ElevatedButton.styleFrom(
                        backgroundColor: Theme.of(context).colorScheme.error,
                        foregroundColor: Colors.white,
                        padding: EdgeInsets.symmetric(vertical: 16.h),
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(8.r),
                        ),
                      ),
                      child: Text(
                        '退出登录',
                        style: TextStyle(
                          fontSize: 16.sp,
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }

  Widget _buildInfoSection(
    BuildContext context, {
    required String title,
    required List<Widget> items,
  }) {
    return Container(
      width: double.infinity,
      decoration: BoxDecoration(
        color: Theme.of(context).colorScheme.surface,
        borderRadius: BorderRadius.circular(12.r),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 10.r,
            offset: Offset(0, 2.h),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
            padding: EdgeInsets.all(16.w),
            child: Text(
              title,
              style: TextStyle(
                fontSize: 16.sp,
                fontWeight: FontWeight.w600,
                color: Theme.of(context).colorScheme.onSurface,
              ),
            ),
          ),
          const Divider(height: 1),
          ...items,
        ],
      ),
    );
  }

  Widget _buildInfoItem(
    BuildContext context, {
    required IconData icon,
    required String title,
    required String value,
    bool showArrow = false,
    VoidCallback? onTap,
  }) {
    return InkWell(
      onTap: onTap,
      child: Container(
        padding: EdgeInsets.all(16.w),
        child: Row(
          children: [
            Icon(
              icon,
              size: 20.w,
              color: Theme.of(context).colorScheme.primary,
            ),
            SizedBox(width: 12.w),
            Expanded(
              child: Text(
                title,
                style: TextStyle(
                  fontSize: 14.sp,
                  color: Theme.of(context).colorScheme.onSurface,
                ),
              ),
            ),
            if (value.isNotEmpty) ...[
              Text(
                value,
                style: TextStyle(
                  fontSize: 14.sp,
                  color: Colors.grey[600],
                ),
              ),
              SizedBox(width: 8.w),
            ],
            if (showArrow)
              Icon(
                Icons.arrow_forward_ios,
                size: 16.w,
                color: Colors.grey[400],
              ),
          ],
        ),
      ),
    );
  }
}